import Vue from 'vue'
import Router from 'vue-router'
import home from '@/components/home'
import document from '@/components/document'
import about from '@/components/about'
import error from '@/components/404'
import study from '@/components/study'
import work from '@/components/work'
import hobby from '@/components/hobby'
import user from '@/components/user'
Vue.use(Router)

let router = new Router({
    mode: 'history',
    routes: [{
            path: '/',
            name: 'home',
            component: home,
            meta:{
              title:home
            }
        },
        {
            path: '/document',
            name: 'document',
            component: document,
            meta:{
              login:true,
              title:document
            }
        },
        {
            path: '/about',
            component: about,
            meta:{
              title:about
            },
            children: [{
                    path: '', // 默认的子路由  /about
                    name: 'About',
                    component: study
                },
                {
                    path: '/about/work', // /work
                    name: 'Work',
                    component: work
                },
                {
                    path: '/about/hobby', // /hobby
                    name: 'Hobby',
                    component: hobby
                }
            ]
        },
        {
            path: '/user/:top?/:userId?',
            name: 'user',
            component: user,
            meta:{
              title:user
            }
        }
        /*{
         path: '*',
         name: '',
         // component: error
         // redirect:'/about'
         redirect:{path:'/home'}
         }*/
    ],

    //滚动行为
    scrollBehavior(to, from, savePosition) {
        console.log(to); // 要进入的目标路由对象 要去向哪里
        console.log(from); // 离开的路由对象  从哪里来
        console.log(savePosition);// 记录滚动条的坐标 点击前进后退的时候记录值

         if(savePosition){
            return savePosition;
         }else{
             return {x:0,y:0}
         }

        // if (to.hash) {
        //     return {
        //         selector: to.hash
        //     }
        // }
    },
});

//全局(路由)导航钩子函数
// router.beforeEach((to,from,next)=>{
//   console.log('beforeEach');
//   //next(false)
//   if(to.meta.login){
//     next('/login')
//   }else{
//     next()
//   }
// });
//
// router.afterEach((to,from)=>{
//   if(to.meta.tile){
//     window.document.title = to.meta.title
//   }else{
//     window.document.title = ''
//   }
// });


export default router
